package im_server

import (
	"errors"
	"go.uber.org/zap"
	"pim/pkg/models"
)

func (c *ClientConn) UserModGetME() (resp *models.UserInfoViewer, err error) {
	// 从数据库读取我的用户信息

	// 从数据库读取
	db := c.svr.db

	var userinfo models.UserInfoViewer

	err = db.Model(&userinfo).Where(&models.UserInfoViewer{
		UserID: c.UserID,
	}).Find(&userinfo).Error

	if err != nil {
		c.logger.Debug("get user info by id fail ", zap.Error(err))
		err = errors.New("user not found")
		return
	}

	resp = &userinfo

	return
}

func (c *ClientConn) UserModGetUserByID(req *models.GetUserByIdReq) (resp *models.UserInfoViewer, err error) {
	db := c.svr.db

	var userinfo models.UserInfoViewer

	err = db.Model(&userinfo).Where(&models.UserInfoViewer{
		UserID: req.UserID,
	}).Find(&userinfo).Error

	if err != nil {
		c.logger.Debug("get user info by id fail ", zap.Error(err))
		err = errors.New("user not found")
		return
	}

	if userinfo.UserID == 0 {
		err = errors.New("user not found")
		return
	}

	resp = &userinfo

	return
}
